/**----------------------------------------------------------
			拖拽 电脑端
-------------------------------------------------------------
 * */
var mDoms = {};
var mCurr = null;

function hGetPx(str) {
	let n = "px";
	let number = '';
	let ix = str.indexOf(n),
		isBegin = false;
	for (var i = ix; i > -1; i--) {
		let c = str[i];
		if (!isNaN(c) || c == '-') {
			isBegin = true;
			number = c + "" + number;
			continue;
		}
		if (isBegin) {
			break;
		}
	};
	return +number;
}

function hMouseDown(e) {
	mCurr = e.currentTarget;

	if (mCurr != e.target) {
		mCurr = null;
		return;
	}
	e.stopPropagation();
	var id = mCurr.id;
	if (!id) {
		id = Date.now() + "_autoid";
		mCurr.id = id;
	}
	if (!mDoms[id]) {

		mDoms[id] = {
			dom: mCurr,
			xy: {
				x: 0,
				y: 0
			}
		}
	}
	let x = 0,
		y = 0;
	let t = mCurr.style.transform;
	if (t) {
		let xy = t.split(",");
		x = hGetPx(xy[0]);
		y = hGetPx(xy[1]);
		mDoms[id].xy = {
			x: x,
			y: y
		}
		console.log("trans:", mDoms[id].xy);
	}
}


function hMouseUp(e) {
	mDoms = {};
	mCurr = null
	//	e.stopPropagation();
	//document.removeEventListener("mouseup", hMouseUp);
	//document.removeEventListener("mousemove", hMove);
}

function hMove(e) {
	if (e.buttons != 1 || mCurr == null) {
		hMouseUp();
		return;
	}
	var odom = mDoms[mCurr?.id];
	if (!odom) {
		hMouseUp();
		return;
	}
	var dom = odom.dom;
	if (!dom) {
		hMouseUp();
		return;
	}
	e.stopPropagation();
	var xy = odom.xy;
	xy = {
		x: xy.x + e.movementX,
		y: xy.y + e.movementY
	};
	odom.xy = xy;
	dom.style.transform = `translate(${xy.x}px,${xy.y}px)`;
	//console.log("trans:", xy, odom.xy);
}
document.addEventListener("mousemove", hMove);
document.addEventListener("mouseup", hMouseUp);